﻿using CPPEI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XWeb.Core.Services
{
    public class QueryService
    {
        public SingleResult<Entities.App> GetApp()
        {
            using (XWebDbContext dbContext = new XWebDbContext())
            {
                EntityRepository<Entities.App> rep = new EntityRepository<Entities.App>(dbContext);
                Entities.App app = rep.Query().SingleOrDefault();
                if(app != null)
                {
                    return new SingleResult<Entities.App>(app);
                }
                else
                {
                    return new SingleResult<Entities.App>(Error.COM_ObjNotExist);
                }
            }
        }

        public ListResult<Data.MediaFolder> GetMediaFolders()
        {
            using (XWebDbContext dbContext = new XWebDbContext())
            {
                List<Data.MediaFolder> roots = new List<Data.MediaFolder>();

                EntityRepository<Entities.MediaFolder> rep = new EntityRepository<Entities.MediaFolder>(dbContext);
                List<Entities.MediaFolder> folders = rep.Query().ToList();

                Stack<Data.MediaFolder> stack = new Stack<Data.MediaFolder>();
                List<Entities.MediaFolder> eRoots = GetRoot(folders);

                for (int index = 0; index < eRoots.Count; index++)
                {
                    Data.MediaFolder folder = new Data.MediaFolder();
                    folder.Folder = eRoots[index];
                    roots.Add(folder);

                    stack.Push(folder);
                }

                return new ListResult<Data.MediaFolder>(roots);
            }
        }

        private List<Entities.MediaFolder> GetRoot(List<Entities.MediaFolder> list)
        {
            List<Entities.MediaFolder> roots = new List<Entities.MediaFolder>();
            for (int index = 0; index < list.Count; )
            {
                if(list[index].ParentId == Guid.Empty)
                {
                    roots.Add(list[index]);
                    list.RemoveAt(index);
                }
                else
                {
                    index++;
                }
            }
            return roots;
        }

        private List<Entities.MediaFolder> GetChildren(Guid parentId, List<Entities.MediaFolder> list)
        {
            List<Entities.MediaFolder> children = new List<Entities.MediaFolder>();
            for (int index = 0; index < list.Count;)
            {
                if (list[index].ParentId == parentId)
                {
                    children.Add(list[index]);
                    list.RemoveAt(index);
                }
                else
                {
                    index++;
                }
            }
            return children;
        }
    }
}
